import 'package:flutter/material.dart';
import '../../common/select_templet.dart';
import '../../common/select_templet_boss.dart';
import '../../routers/application.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../config/service_method.dart';
import 'dart:convert';
import '../../common/ok_toast.dart';

class ExamList extends StatefulWidget{
    @override
    _ExamListState createState() => _ExamListState();
}
class _ExamListState extends State<ExamList>{

    bool selectFilter = false;
    String selectValue;    //年级筛选的value值
    List _selectValLab = [{}];
    List _examListContent = [];
    String _studentGradeId = "";

    initState(){
        super.initState();
        getStudentGradeId();
    }

    Widget build(BuildContext context){
        return Scaffold(
            appBar: AppBar(
                title: Text("成绩列表"),
                centerTitle: true,
                actions: <Widget>[
                    IconButton(
                        icon: Icon(IconData(0xe622, fontFamily: "Iconfont"), size: 24.0),
                        onPressed: (){
                            if(selectFilter == true){
                                eventBus.fire(SelectTemplet(openClose: false));
                                    selectFilter = false;
                            }else{
                                eventBus.fire(SelectTemplet(openClose: true));
                                    selectFilter = true;
                            }
                        },
                    )
                ],
            ),
            body: Container(
                child: Stack(
                    // overflow: Overflow.clip,
                    children: <Widget>[
                        Positioned(
                            child: Container(
                                child: ListView.builder(
                                    itemCount: _examListContent.length,
                                    itemBuilder: (BuildContext context,int index){
                                        return InkWell(
                                            onTap: (){
                                                //如果直接url携带参数则需要encode，直接使用SharedPreferences存储
                                                String examName = _examListContent[index]['analysisName'].toString();
                                                Future<dynamic> future = Future(()async{
                                                    SharedPreferences prefs =await SharedPreferences.getInstance();
                                                    prefs.setString("examName",examName);
                                                    return prefs.getString("roleId");
                                                });
                                                future.then((val){
                                                    var analysisId = _examListContent[index]['analysisId'].toString();
                                                    Application.router.navigateTo(context,"/examList/examResult?analysisId=$analysisId&schoolYearStart=$selectValue&roleId=$val");
                                                }).catchError((e){
                                                    print(e);
                                                });
                                            },
                                            child: Container(
                                                height: 70.0,
                                                padding: EdgeInsets.all(5.0),
                                                decoration: new UnderlineTabIndicator(
                                                    borderSide: BorderSide(width: 1.0, color: Colors.grey),
                                                    // insets: EdgeInsets.fromLTRB(0,0,0,10)
                                                ),
                                                child: Row(
                                                    children: <Widget>[
                                                        Expanded(
                                                            child: Column(
                                                                children: <Widget>[
                                                                    Expanded(
                                                                        child: Container(
                                                                            alignment: Alignment.centerLeft,
                                                                            child: Text(_examListContent[index]['analysisName'].toString()),
                                                                        ),
                                                                    ),
                                                                    Expanded(
                                                                        child: Container(
                                                                            alignment: Alignment.centerLeft,
                                                                            child: Text("年级:"+_examListContent[index]['gradeName'].toString()+"        "+"考试时间:"+_examListContent[index]['time'].toString()),
                                                                        ),
                                                                    )
                                                                ],
                                                            ),
                                                        ),
                                                        Container(
                                                            child: Icon(Icons.keyboard_arrow_right),
                                                        )
                                                    ],
                                                ),
                                            ),
                                        );
                                    },
                                ),
                            ),
                        ),
                        Container(
                            child: Column(
                                children: <Widget>[
                                    SelectTemplet(
                                        title: "年级筛选:",
                                        selectValLab:_selectValLab,
                                        callback: (value,label){
                                            selectValue = value;
                                            print("----id:---${label}------${value}");
                                            _getExamList(value);
                                            selectFilter = false;
                                            eventBus.fire(SelectTemplet(openClose: false));
                                        }
                                    ),
                                    
                                ],
                            ),
                        ),
                       
                    ],
                )
            ),
        );
    }



    //获取学生的年级等信息
    //gradeId: "c1f7bce7708e4c08a6a1d2022ff3c874"
    //studentClass: "高三(11)班"
    //studentGrade: "高三"
    //studentId: "137c32385ec645609dff82ff809ef05d"
    //studentName: "桂睿昊"
    //studentNo: "31519010563LD"
    void getStudentGradeId()async{
        Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("roleId");
            });
            future.then((val)async{
                var data = {
                    "roleId" :val ,
                };
                await DioUtil.request(context,'/tqes/web/squaAnalysisExamDetail/getStudentByNameGradeClass',formData: data).then((val){
                    Map<String,dynamic> responseData = json.decode(val.toString());
                    // print(responseData["data"]);
                    if(responseData["success"]){
                        _studentGradeId = responseData["data"]["gradeId"];
                        getStudyYearList();
                    }else{
                        OkToast.singleToast(responseData["msg"]);
                    }

                });
            }).catchError((_){
                print("catchError");
            });
    }


    //获取学年的选择列表
    void getStudyYearList(){
        Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("roleId");
            });
            future.then((val)async{
                var data = {
                    "roleId" :val ,
                };
                await DioUtil.request(context,'/tqes/web/squaAnalysisExamDetail/getStudentSchoolYearSelect',formData: data).then((val){
                    Map<String,dynamic> responseData = json.decode(val.toString());
                    // print(responseData);
                    if(responseData["success"]){
                        setState(() {
                            _selectValLab = responseData["data"];
                        });
                    }else{
                         OkToast.singleToast(responseData["msg"]);
                    }

                });
            }).catchError((_){
                print("catchError");
            });
    }

    void _getExamList(String schoolYearStartValue){
        Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("roleId");
            });
            future.then((val)async{
                var data = {
                    "current": 1,
                    "gradeId": _studentGradeId,
                    "pageSize": "10",
                    "roleId": val,
                    "schoolYearStart": schoolYearStartValue,
                };
                await DioUtil.request(context,'/tqes/web/squaAnalysisExamDetail/getStudentQueryAnalysisList',formData: data).then((val){
                    Map<String,dynamic> responseData = json.decode(val.toString());
                    // print(responseData["data"]);
                    if(responseData["success"]){
                        setState(() {
                          _examListContent = responseData["data"]["items"];
                        });
                        
                    }else{
                         OkToast.singleToast(responseData["msg"]);
                    }

                });
            }).catchError((_){
                print("catchError");
            });
    }
}

